﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Lib.CommonForm;

namespace AUI
{
    public partial class frmRoleInfo : FormBase
    {
        string _RoleId = "";
        string _RoleName = "";
        public frmRoleInfo(string roleId, string roleName)
        {
            InitializeComponent();
            

            this.txtRoleName.MaxLength = tbl_Roles_Length.RoleName;

            _listRole = tbl_RoleRight_Controller.Instance().GetInfo("VaiTro");
            _RoleId = roleId;
            _RoleName = roleName;

            //
            txtRoleName.Text = _RoleName;
            FormGlobals.Control_SetRequired(txtRoleName);

            FormGlobals.dgv_SelectionMode_CellSelect(dgvRoleRights, false);
            FormGlobals.dgv_AddColumn_TextBox(dgvRoleRights, "ModuleName", "Tên chức năng", "", 400, true, true);
            FormGlobals.dgv_AddColumn_CheckBox(dgvRoleRights, "ViewRight", "Xem", "", 80, true, false);
            FormGlobals.dgv_AddColumn_CheckBox(dgvRoleRights, "CreateRight", "Thêm", "", 60, true, false);
            FormGlobals.dgv_AddColumn_CheckBox(dgvRoleRights, "UpdateRight", "Sửa", "", 60, true, false);
            FormGlobals.dgv_AddColumn_CheckBox(dgvRoleRights, "DeleteRight", "Xóa", "", 60, true, false);
            FormGlobals.dgv_AddColumn_CheckBox(dgvRoleRights, "PrintRight", "Xuất", "", 60, true, false);
            FormGlobals.dgv_AddColumn_CheckBox(dgvRoleRights, "ModuleID", "ModuleID", "", 60, false, false);

            FormGlobals.dgv_ColumnsSortMode(dgvRoleRights, DataGridViewColumnSortMode.NotSortable);
            dgvRoleRights.Columns["ModuleName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;

            dgvRoleRights.DataSource = tbl_RoleRight_Controller.Instance().GetByRoleName(_RoleName);
            //

        }

        public override void Show_Form()
        {
            this.ShowDialog();
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            string roleName = txtRoleName.Text.Trim();
            txtRoleName.BackColor = SystemColors.Window;

            if (String.IsNullOrEmpty(roleName))
            {
                txtRoleName.BackColor = Color.Yellow;
                FormGlobals.Message_Warning("Vai trò không được để trống.");
                txtRoleName.Focus();
                return;
            }

            if (_RoleId.Length == 0)
            {
                if (!tbl_Roles_Controller.Instance().RoleExists(roleName))
                {
                    tbl_Roles_Controller.Instance().CreateRole(roleName);
                    _RoleId = Globals.ToString(DataConnect.ExecuteScalar("SELECT RoleId FROM aspnet_Roles WHERE RoleName = N'" + roleName + "'"));
                }
                else
                {
                    txtRoleName.BackColor = Color.Yellow;
                    FormGlobals.Message_Warning("Vai trò này đã tồn tại trong hệ thống.");
                    txtRoleName.Focus();
                    return;
                }

            }


            
            try
            {
                //Update RoleName
                tbl_Roles_Controller.Instance().UpdateRole(_RoleId, roleName);

                //Update RoleRights
                DataTable dt = (dgvRoleRights.DataSource as DataTable).GetChanges();

                if (dt != null)
                {
                    tbl_RoleRight_Info info;

                    foreach (DataRow dr in dt.Rows)
                    {
                        info = new tbl_RoleRight_Info();
                        info.FillDataProperty(dr);

                        if (dr.RowState == DataRowState.Deleted || dr.RowState == DataRowState.Modified)
                        {
                            //Xóa
                            tbl_RoleRight_Controller.Instance().Delete(info.RoleId, info.ModuleId);
                        }

                        if (dr.RowState == DataRowState.Modified || dr.RowState == DataRowState.Added)
                        {
                            //Thêm hoặc thêm lại cái đã xóa coi như sửa
                            tbl_RoleRight_Controller.Instance().Insert(info);
                        }
                    }
                    (dgvRoleRights.DataSource as DataTable).AcceptChanges();
                    //

                }
                //else FormGlobals.Message_Information(this, "Không có dữ liệu thay đổi");
            }
            catch (Exception ex)
            {
                FormGlobals.Message_Error(ex);
            }
            FormGlobals.Message_InTaskbar(UIMessage.SAVE_COMPLETE);
            _dlgResult = DialogResult.OK;
            this.Close();
        }


        private void dgvRoleRights_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            //DataTable dt = (DataTable)dgvRoleRights.DataSource;

            //    for (int nRow = 0; nRow < dt.Rows.Count; nRow++)
            //    {
            //        if (dt.Rows[nRow].HasVersion(DataRowVersion.Proposed))
            //        {
            //            dt.Rows[nRow].EndEdit();
            //        }
            //    }

        }
    }
}
